package com.steerinfo.dil.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtils {
    /**
     * 日期转换为字符串 格式自定义
     *
     * @param date
     * @param timeformat
     * @return
     */
    public static String dateStr(Date date, String timeformat) {
        if (date == null) {
            return "";
        }
        String str = new SimpleDateFormat(timeformat).format(date);
        return str;
    }

    public static String dateStr(Date date) {
        if (date == null) {
            return "";
        }
        String str = new SimpleDateFormat("yyyy-mm-dd").format(date);
        return str;
    }
    /**
     * 字符串转换为日期 格式自定义
     *
     * @param date
     * @param date
     * @return
     */
    public static Date StrDate(String date) {
        SimpleDateFormat dateTimeFormat = new SimpleDateFormat ("yyyy-MM-dd");
        String str = new SimpleDateFormat("yyyy-mm-dd").format(date);
        try {
            Date date1 = dateTimeFormat.parse(date);
            return date1;
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }

    }

    /**
     * 时间戳转标准时间
     *
     * @param stap   时间戳
     * @return
     */
    public static String stampToDate(String stap){
        String time;
        try {
            if (stap == null || stap.length() == 0) {
                time = null;
            } else {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date date = null;
                try {
                	long lt = new Long(stap);
                	date = new Date(lt);
                }
                catch(Exception e) {
                	date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(stap);
                }
                time = simpleDateFormat.format(date);
            }
        } catch (Exception e) {
            time = null;
            e.printStackTrace();
        }
        return time;
    }

    /**
     * 时间序列偏移量 偏移量自定义
     *
     * @param time
     * @param offsetinfo 偏移量
     * @return
     */
    public static Date offsetDate(Date time,String offsetinfo) {
        String[] offsetinfoarray = offsetinfo.split(",");
        Calendar cal = Calendar.getInstance();
        cal.setTime(time);
        for (int i =0;i < offsetinfoarray.length;i++){
            if(offsetinfoarray[i] != null && !"".equals(offsetinfoarray[i])){
                String timegran = offsetinfoarray[i].substring(0, offsetinfoarray[i].length() - 2);
                int offset = Integer.parseInt(offsetinfoarray[i].substring(offsetinfoarray[i].length() - 2));
                switch (timegran){
                    case "ss":
                        cal.add(Calendar.SECOND, offset);
                        break;
                    case "mm":
                        cal.add(Calendar.MINUTE, offset);
                        break;
                    case "HH":
                        cal.add(Calendar.HOUR, offset);
                        break;
                    case "dd":
                        cal.add(Calendar.DATE, offset);
                        break;
                    case "MM":
                        cal.add(Calendar.MONTH, offset);
                        break;
                    case "yyyy":
                        cal.add(Calendar.YEAR, offset);
                        break;
                }
            }
        }
        return cal.getTime();
    }

    public static List<String> getMonitortime(String timegran, String time, String starttime) {
        List<String> list = new ArrayList<String>();
        if("YEAR".equals(timegran)){
            list.add(time);
        }
        else{
            try {
                Date d1 = new SimpleDateFormat("yyyy-MM-dd").parse(starttime);//定义开始日期
                Date d2 = new SimpleDateFormat("yyyy-MM-dd").parse((Integer.parseInt(time) + 1) + "-01-01");//定义结束日期
                Calendar dd = Calendar.getInstance();//定义日期实例
                dd.setTime(d1);//设置日期起始时间
                while(dd.getTime().before(d2)) {//判断是否到结束日期
                    if ("MONTH".equals(timegran)) {
                        list.add(dateStr(dd.getTime(), "yyyy-MM"));
                        dd.add(Calendar.MONTH, 1);
                    }
                    else{
                        list.add(dateStr(dd.getTime(), "yyyy-MM-dd"));
                        dd.add(Calendar.DAY_OF_YEAR, 1);
                    }
                }
            }
            catch (ParseException e){
                e.printStackTrace();
            }
        }
        return list;
    }

    /**
     * 自动补全日期并返回新日期 偏移量自定义
     *
     * @param time 时间
     * @param interval 偏移量
     * @return
     */
    public static String changeTime(String time,int interval){
        Calendar dd = Calendar.getInstance();
        try{
            if(time.length() == 4){
                Date d = new SimpleDateFormat("yyyy-MM-dd").parse(time+"-01-01");
                dd.setTime(d);
                dd.add(Calendar.YEAR, interval);
            }
            if(time.length() == 7){
                Date d = new SimpleDateFormat("yyyy-MM-dd").parse(time+"-01");
                dd.setTime(d);
                dd.add(Calendar.MONTH, interval);
            }
            if(time.length() == 10){
                Date d = new SimpleDateFormat("yyyy-MM-dd").parse(time);
                dd.setTime(d);
                dd.add(Calendar.DATE, interval);
            }
        }
        catch (ParseException e){
            e.printStackTrace();
        }
        return dateStr(dd.getTime(), "yyyy-MM-dd");
    }
    /**
     * 获取现在时间
     * @Param timeformat 时间格式
     * @return（String）格式自定义
     */
    public static String getCurrentTime(String timeformat){
        Date time = new Date();
        return dateStr(time, timeformat);
    }

    /**
     * 获取现在时间
     * @Param timeformat 时间格式
     * @return（Date）格式自定义
     */
    public static Date getCurrentTimetoDate(String timeformat) {
        try {
            Date date = new java.sql.Date(new Date().getTime());
            String datestr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
            Date time = new SimpleDateFormat(timeformat).parse(datestr);
            return time;
        }
        catch (ParseException e){
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 将"yyyy-MM-dd"转换为 格式自定义
     *
     * @param date
     * @param timeformat
     * @return
     */
    public static String dateToString(String date, String timeformat) {
        String Strtime = "";
        try {
            if(date != null && !"".equals(date)) {
                Date parse = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date + " 00:00:00");
                Strtime = new SimpleDateFormat(timeformat).format(parse);
            }
        }
        catch (ParseException e) {
            e.printStackTrace();
        }
        return Strtime;
    }
    /**
     * 判断某天是否是该月的第一天
     * @param  date
     * @return
     */
    public static boolean isFirstDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        System.out.println(calendar.get(Calendar.MONTH));
        return calendar.get(Calendar.DAY_OF_MONTH) == 1;
    }
    public static String getDate(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date dt = null;
        String reStr = null;
        try {
            dt = sdf.parse(DateUtils.dateStr(new Date(),"yyyy-MM-dd"));
            Calendar rightNow = Calendar.getInstance();
            rightNow.setTime(dt);
            rightNow.add(Calendar.DAY_OF_MONTH, -3);
            Date dt1 = rightNow.getTime();
             reStr = sdf.format(dt1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return reStr ;
    }

    /**
     * 根据月份得到当月天数
     * @param date
     * @return
     * @throws ParseException
     */
    public static int getDaysOfMonth(String date) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(sdf.parse(date));
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }
    /**
     * 根据月份得到当月天数
     * @param date
     * @return
     * @throws ParseException
     */
    public static int getDaysOfMonth(Date date) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }
}